{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test and visualize the driving scenarios"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Method definition info(Any...) in module Base at util.jl:532 overwritten in module Logging at /mnt/c/Users/Maxime/wsl/.julia/v0.6/Logging/src/Logging.jl:115.\n",
      "WARNING: Method definition warn(Any...) in module Base at util.jl:585 overwritten in module Logging at /mnt/c/Users/Maxime/wsl/.julia/v0.6/Logging/src/Logging.jl:115.\n",
      "WARNING: Method definition copyto!(Array{Float64, 1}, AutomotiveDrivingModels.LatLonAccel) in module AutomotiveDrivingModels at /mnt/c/Users/Maxime/wsl/.julia/v0.6/AutomotiveDrivingModels/src/2d/actions/lat_lon_accel.jl:13 overwritten in module AutoUrban at /mnt/c/Users/Maxime/wsl/.julia/v0.6/AutoUrban/src/simulation/actions.jl:10.\n",
      "WARNING: Method definition (::Type{AutomotivePOMDPs.LidarOverlay})(AutomotiveDrivingModels.LidarSensor) in module AutomotivePOMDPs at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/driver_models/lidar_sensor.jl:46 overwritten at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/multi_crosswalk/render_helpers.jl:32.\n",
      "WARNING: Method definition (::Type{AutomotivePOMDPs.LidarOverlay})(Any) in module AutomotivePOMDPs at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/driver_models/lidar_sensor.jl:46 overwritten at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/multi_crosswalk/render_helpers.jl:32.\n",
      "WARNING: Method definition render_ray!(AutoViz.RenderModel, Vec.VecSE2{R} where R<:Real) in module AutomotivePOMDPs at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/driver_models/lidar_sensor.jl:56 overwritten at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/multi_crosswalk/render_helpers.jl:87.\n",
      "WARNING: Method definition #render_ray!(Array{Any, 1}, typeof(AutomotivePOMDPs.render_ray!), AutoViz.RenderModel, Vec.VecSE2{R} where R<:Real) in module AutomotivePOMDPs overwritten.\n",
      "WARNING: Method definition render_lidar!(AutoViz.RenderModel, AutomotiveDrivingModels.LidarSensor, Vec.VecSE2{R} where R<:Real) in module AutomotivePOMDPs at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/driver_models/lidar_sensor.jl:66 overwritten at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/multi_crosswalk/render_helpers.jl:97.\n",
      "WARNING: Method definition #render_lidar!(Array{Any, 1}, typeof(AutomotivePOMDPs.render_lidar!), AutoViz.RenderModel, AutomotiveDrivingModels.LidarSensor, Vec.VecSE2{R} where R<:Real) in module AutomotivePOMDPs overwritten.\n",
      "WARNING: Method definition render!(AutoViz.RenderModel, AutomotivePOMDPs.LidarOverlay, Records.Frame{Records.Entity{AutomotiveDrivingModels.VehicleState, AutomotiveDrivingModels.VehicleDef, Int64}}, AutomotivePOMDPs.CrosswalkEnv) in module AutomotivePOMDPs at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/driver_models/lidar_sensor.jl:78 overwritten at /mnt/c/Users/Maxime/OneDrive - Leland Stanford Junior University/Research/AutomotivePOMDPs/multi_crosswalk/render_helpers.jl:109.\n"
     ]
    }
   ],
   "source": [
    "include(\"AutomotivePOMDPs.jl\")\n",
    "using AutomotivePOMDPs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Method definition info(Any...) in module Base at util.jl:532 overwritten in module Logging at C:\\Users\\Maxime\\.julia\\v0.6\\Logging\\src\\Logging.jl:115.\n",
      "WARNING: Method definition warn(Any...) in module Base at util.jl:585 overwritten in module Logging at C:\\Users\\Maxime\\.julia\\v0.6\\Logging\\src\\Logging.jl:115.\n",
      "WARNING: Method definition copyto!(Array{Float64, 1}, AutomotiveDrivingModels.LatLonAccel) in module AutomotiveDrivingModels at C:\\Users\\Maxime\\.julia\\v0.6\\AutomotiveDrivingModels\\src\\2d\\actions\\lat_lon_accel.jl:13 overwritten in module AutoUrban at C:\\Users\\Maxime\\.julia\\v0.6\\AutoUrban\\src\\simulation\\actions.jl:10.\n"
     ]
    }
   ],
   "source": [
    "using POMDPs, StatsBase, POMDPToolbox, RLInterface, Parameters, GridInterpolations\n",
    "using AutomotiveDrivingModels, AutoUrban, AutoViz\n",
    "using Reel "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rng = MersenneTwister(1);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Occluded Crosswalk POMDP"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load default POMDP type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pomdp = SingleOCPOMDP();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a policy and a belief updater"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "policy = solve(RandomSolver(rng), pomdp)\n",
    "up = updater(policy);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulate and visualize a run of the policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  2.907097 seconds (1.41 M allocations: 70.199 MiB, 1.00% gc time)\n"
     ]
    }
   ],
   "source": [
    "hr = HistoryRecorder(rng=rng)\n",
    "@time hist = simulate(hr, pomdp, policy, up);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<video autoplay controls><source src=\"files/reel-7965295342006923667.webm?9761369729997795307\" type=\"video/webm\"></video>"
      ],
      "text/plain": [
       "Reel.Frames{MIME{Symbol(\"image/png\")}}(\"/tmp/tmpOpjzZY\", 0x000000000000000c, 2.0, nothing)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "duration, fps, render_hist = animate_hist(pomdp, hist)\n",
    "film = roll(render_hist, fps = fps, duration = duration)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Occluded Intersection POMDP"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load default POMDP type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pomdp = SingleOIPOMDP();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a policy and a belief updater"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "policy = solve(RandomSolver(rng), pomdp)\n",
    "up = updater(policy);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulate and visualize a run of the policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  0.858034 seconds (354.50 k allocations: 16.196 MiB, 1.00% gc time)\n"
     ]
    }
   ],
   "source": [
    "hr = HistoryRecorder(rng=rng)\n",
    "@time hist = simulate(hr, pomdp, policy, up);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<video autoplay controls><source src=\"files/reel-3992929184936811883.webm?6163273752799302191\" type=\"video/webm\"></video>"
      ],
      "text/plain": [
       "Reel.Frames{MIME{Symbol(\"image/png\")}}(\"/tmp/tmpGIFkJE\", 0x000000000000000e, 2.0, nothing)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "duration, fps, render_hist = animate_hist(pomdp, hist)\n",
    "film = roll(render_hist, fps = fps, duration = duration)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Occluded Crosswalk POMDP"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load default POMDP type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pomdp = OCPOMDP();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a policy and a belief updater"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "POMDPToolbox.PreviousObservationUpdater{Array{Float64,1}}()"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "policy = solve(RandomSolver(rng), pomdp)\n",
    "up = updater(policy);\n",
    "up = PreviousObservationUpdater{obs_type(pomdp)}()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulate and visualize a run of the policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  1.217501 seconds (108.98 k allocations: 5.325 MiB)\n"
     ]
    }
   ],
   "source": [
    "hr = HistoryRecorder(rng=rng)\n",
    "@time hist = simulate(hr, pomdp, policy, up);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<video autoplay controls><source src=\"files/reel-15299319777713385322.webm?5238884146964326242\" type=\"video/webm\"></video>"
      ],
      "text/plain": [
       "Reel.Frames{MIME{Symbol(\"image/png\")}}(\"/tmp/tmpYfZfbL\", 0x0000000000000010, 2.0, nothing)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "duration, fps, render_hist = animate_hist(pomdp, hist)\n",
    "film = roll(render_hist, fps = fps, duration = duration)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Occluded Intersection POMDP"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load default POMDP type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "LoadError",
     "evalue": "\u001b[91mUndefVarError: OIPOMDP not defined\u001b[39m",
     "output_type": "error",
     "traceback": [
      "\u001b[91mUndefVarError: OIPOMDP not defined\u001b[39m",
      "",
      "Stacktrace:",
      " [1] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m"
     ]
    }
   ],
   "source": [
    "pomdp = OIPOMDP();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a policy and a belief updater"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "POMDPToolbox.PreviousObservationUpdater{Array{Float64,1}}()"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "policy = solve(RandomSolver(rng), pomdp)\n",
    "up = updater(policy);\n",
    "up = PreviousObservationUpdater{obs_type(pomdp)}()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulate and visualize a run of the policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  1.411177 seconds (502.23 k allocations: 21.934 MiB, 1.28% gc time)\n"
     ]
    }
   ],
   "source": [
    "hr = HistoryRecorder(rng=rng)\n",
    "@time hist = simulate(hr, pomdp, policy, up);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<video autoplay controls><source src=\"files/reel-970226289828226425.webm?16349836992475684495\" type=\"video/webm\"></video>"
      ],
      "text/plain": [
       "Reel.Frames{MIME{Symbol(\"image/png\")}}(\"/tmp/tmpyuX2sl\", 0x0000000000000020, 2.0, nothing)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "duration, fps, render_hist = animate_hist(pomdp, hist)\n",
    "film = roll(render_hist, fps = fps, duration = duration)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Urban POMDP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Method definition info(Any...) in module Base at util.jl:532 overwritten in module Logging at /mnt/c/Users/Maxime/wsl/.julia/v0.6/Logging/src/Logging.jl:115.\n",
      "WARNING: Method definition warn(Any...) in module Base at util.jl:585 overwritten in module Logging at /mnt/c/Users/Maxime/wsl/.julia/v0.6/Logging/src/Logging.jl:115.\n",
      "WARNING: Method definition copyto!(Array{Float64, 1}, AutomotiveDrivingModels.LatLonAccel) in module AutomotiveDrivingModels at /mnt/c/Users/Maxime/wsl/.julia/v0.6/AutomotiveDrivingModels/src/2d/actions/lat_lon_accel.jl:13 overwritten in module AutoUrban at /mnt/c/Users/Maxime/wsl/.julia/v0.6/AutoUrban/src/simulation/actions.jl:10.\n"
     ]
    }
   ],
   "source": [
    "include(\"AutomotivePOMDPs.jl\")\n",
    "using AutomotivePOMDPs\n",
    "rng = MersenneTwister(1)\n",
    "using POMDPs, POMDPToolbox, AutomotiveDrivingModels, AutoViz, Reel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load default POMDP type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pomdp = UrbanPOMDP(lidar=true);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a policy and a belief updater"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "policy = solve(RandomSolver(rng), pomdp)\n",
    "up = updater(policy);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simulate and visualize a run of the policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  0.194837 seconds (1.76 M allocations: 40.466 MiB, 40.13% gc time)\n"
     ]
    }
   ],
   "source": [
    "hr = HistoryRecorder(rng=rng)\n",
    "@time hist = simulate(hr, pomdp, policy, up);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<video autoplay controls><source src=\"files/reel-590684849762881646.webm?16873653950842163394\" type=\"video/webm\"></video>"
      ],
      "text/plain": [
       "Reel.Frames{MIME{Symbol(\"image/png\")}}(\"/tmp/tmpbEgdK8\", 0x0000000000000010, 2.0, nothing)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lidar_hist = hist.info_hist\n",
    "insert!(lidar_hist, 1, hist.info_hist[1])\n",
    "duration, fps, render_hist = animate_hist(pomdp, hist.state_hist, lidar_hist)\n",
    "film = roll(render_hist, fps = fps, duration = duration)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
